package org.example;


/*
 *  语法:
 *  grammar NestedNameList;
 *  list     : '[' elements ']' ;         // 匹配方括号
 *  elements : element (',' element)* ;   // 匹配list的逗号
 *  element  : NAME | list ;              // element是NAME或嵌套的list
 *  NAME     : ('a'..'z' | 'A'..'Z')+ ;   // NAME含有至少一个字母
 * */
public class ListParser extends Parser {
    public ListParser(Lexer input) {
        super(input);
    }

    public void list() {
        match(ListLexer.LBRACK);
        elements();
        match(ListLexer.RBRACK);
    }

    public void elements() {
        element();
        while (lookahead.type == ListLexer.COMMA) {
            match(ListLexer.COMMA);
            element();
        }
    }

    public void element() {
        if (lookahead.type == ListLexer.NAME) {
            match(ListLexer.NAME);
        } else if (lookahead.type == ListLexer.LBRACK) {
            list();
        } else {
            throw new Error("期望输入name或list，但是输入为: " + lookahead);
        }
    }
}
